#include "stdafx.h"
#include "RectSelect.h"
#include "RectElement.h"
#include "PFMEngine.h"



RectSelect::RectSelect()
:m_pRectBase(NULL)
,m_pRectSelect(NULL)
,m_bSelected(FALSE)
{	
}

RectSelect::~RectSelect()
{
	GetPFMEngine().ReleaseRect(m_pRectBase);
	GetPFMEngine().ReleaseRect(m_pRectSelect);		
}

void RectSelect::ChangeSize()
{
	assert(m_pRectBase != NULL);

	m_pRectBase->SetPos(float(m_nPosX), float(m_nPosY), float(m_nWidth), float(m_nHeight), m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
}

void RectSelect::Create(RectElement* pRectBase, RectElement* pRectSelect, int nPosX, int nPosY, int nWidth, int nHeight)
{
	m_pRectBase = pRectBase;
	m_pRectSelect = pRectSelect;
	m_nPosX = nPosX;
	m_nPosY = nPosY;
	m_nWidth = nWidth;
	m_nHeight = nHeight;
	ChangeSize();
}













RectSelectScale::RectSelectScale()
:m_fMaxScale(0)
,m_nScaleCounter(0)
,m_nDestPosX(0)
,m_nDestPosY(0)
,m_bShowSelectRect(FALSE)
,m_fSelectPosX(0)
,m_fSelectPosY(0)
,m_fSelectWidth(0)
,m_fSelectHeight(0)
,m_nMaxCounter(10)
{
}

RectSelectScale::~RectSelectScale()
{
}

void RectSelectScale::Update()
{
	if(!m_bShow)	return;

	if( IsSelected() )
	{
		++m_nScaleCounter;
		if( m_nScaleCounter < m_nMaxCounter )
		{
			float fScaleRate = (m_fMaxScale * sinf((((float(m_nScaleCounter)/m_nMaxCounter) * 90.f)/180.f) * 3.141591f) + 1);
			float fWidth = float(m_nWidth) * fScaleRate;
			float fHeight = float(m_nHeight)  * fScaleRate;
			float fPosX = float(m_nPosX) - (fWidth - float(m_nWidth))*0.5f;
			float fPosY = float(m_nPosY) - (fHeight - float(m_nHeight))*0.5f;
			m_pRectBase->SetPos(fPosX, fPosY, fWidth, fHeight, m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
		}
		else
		{
			m_nScaleCounter = m_nMaxCounter;
		}
	}
	else
	{
		--m_nScaleCounter;
		if( m_nScaleCounter >= 0 )
		{
			float fScaleRate = (m_fMaxScale * sinf((((float(m_nScaleCounter)/m_nMaxCounter) * 90.f)/180.f) * 3.141591f) + 1);
			float fWidth = float(m_nWidth) * fScaleRate;
			float fHeight = float(m_nHeight)  * fScaleRate;
			float fPosX = float(m_nPosX) - (fWidth - float(m_nWidth))*0.5f;
			float fPosY = float(m_nPosY) - (fHeight - float(m_nHeight))*0.5f;			
			m_pRectBase->SetPos(fPosX, fPosY, fWidth, fHeight, m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
		}
		else
		{
			m_nScaleCounter = -1;
		}
	}
	GetPFMEngine().AddDrawElement(m_pRectBase);

	if( m_bShowSelectRect )
	{		
		m_fSelectAlpah -= 0.05f;
		if( m_fSelectAlpah <= 0 )
		{
			m_bShowSelectRect = FALSE;
		}
		else
		{
			m_fSelectPosX += (m_nDestPosX - m_fSelectPosX) * 0.3f;
			m_fSelectPosY += (m_nDestPosY - m_fSelectPosY) * 0.3f;
			m_fSelectWidth *= 0.95f;
			m_fSelectHeight *= 0.95f;
			m_pRectSelect->SetPos(m_fSelectPosX, m_fSelectPosY, m_fSelectWidth, m_fSelectHeight, m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom, m_fSelectAlpah);
			GetPFMEngine().AddDrawElement(m_pRectSelect);
		}		
	}
}

void RectSelectScale::Reset()
{
	OffSelectRect();
	m_bSelected = FALSE;
	m_nScaleCounter = 0;
	m_pRectBase->SetPos(float(m_nPosX), float(m_nPosY), float(m_nWidth), float(m_nHeight), m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
}

void RectSelectScale::OffSelectRect()
{
	m_bShowSelectRect = FALSE;
}

void RectSelectScale::Create(RectElement* pRectBase, RectElement* pRectSelect,
							 int nPosX, int nPosY, int nWidth, int nHeight, 
							 float fmaxScale)
{
	m_fMaxScale = fmaxScale - 1.f;	
	RectSelect::Create(pRectBase, pRectSelect, nPosX, nPosY, nWidth, nHeight);

}

void RectSelectScale::SetTexture(const char* pPath)
{
	assert(m_pRectBase != NULL && m_pRectSelect != NULL );

	GetPFMEngine().ChangeRectTexture(m_pRectBase, pPath);	
	GetPFMEngine().ChangeRectTexture(m_pRectSelect, pPath);	
}

void RectSelectScale::SetClickDown()
{
	m_bShowSelectRect = TRUE;

	float fScaleRate = (m_fMaxScale * sinf((((float(m_nScaleCounter)/m_nMaxCounter) * 90.f)/180.f) * 3.141591f) + 1);
	m_fSelectWidth = (m_nWidth*fScaleRate - m_nWidth);
	m_fSelectHeight = (m_nHeight*fScaleRate - m_nWidth);
	m_fSelectPosX = float(m_nPosX) - (m_fSelectWidth - m_nWidth)*0.5f;
	m_fSelectPosY = float(m_nPosY) - (m_fSelectHeight - m_nHeight)*0.5f;
	m_fSelectAlpah = 1.f;
	m_pRectSelect->SetPos(m_fSelectPosX, m_fSelectPosY, m_fSelectWidth, m_fSelectHeight, m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom, m_fSelectAlpah);
}

















RectSelectButton::RectSelectButton()
:m_pRectCurDraw(NULL)
,m_bMouseDown(FALSE)
,m_pRectMouseDown(0)
{}

RectSelectButton::~RectSelectButton()
{
	GetPFMEngine().ReleaseRect(m_pRectMouseDown);		
}

void RectSelectButton::ChangeSize()
{
	__super::ChangeSize();

	m_pRectSelect->SetPos(float(m_nPosX), float(m_nPosY), float(m_nWidth), float(m_nHeight), m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
	m_pRectMouseDown->SetPos(float(m_nPosX), float(m_nPosY), float(m_nWidth), float(m_nHeight), m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
}

void RectSelectButton::Update()
{
	if( IsShow() == FALSE ) return;

	m_bMouseUp = FALSE;

	if( IsClicked() )
	{
		m_pRectCurDraw = m_pRectMouseDown;	
	}
	else if( IsSelected() )
	{
		m_pRectCurDraw = m_pRectSelect;		
	}
	else
	{
		m_pRectCurDraw = m_pRectBase;
	}
	GetPFMEngine().AddDrawElement(m_pRectCurDraw);	
}

void RectSelectButton::Reset()
{
	m_bSelected = FALSE;
	m_bMouseDown = FALSE;
	m_bMouseUp = FALSE;
}

void RectSelectButton::Create( RectElement* pRectBase, RectElement* pRectSelect, RectElement* pRectMouseDown,int nPosX, int nPosY, int nWidth, int nHeight)
{
	m_pRectMouseDown = pRectMouseDown;
	RectSelect::Create(pRectBase, pRectSelect, nPosX, nPosY, nWidth, nHeight);	

	m_pRectCurDraw = pRectBase;
}

void RectSelectButton::SetTexture(int nIndex, const char* pPath)
{
	if(nIndex == 0)
	{
		GetPFMEngine().ChangeRectTexture(m_pRectBase, pPath);	
	}
	else if(nIndex == 1)
	{
		GetPFMEngine().ChangeRectTexture(m_pRectSelect, pPath);
	}
	else if(nIndex == 2)
	{
		GetPFMEngine().ChangeRectTexture(m_pRectMouseDown, pPath);
	}
}

void RectSelectButton::SetClickDown()
{
	m_bMouseDown = TRUE;
	m_bMouseUp = FALSE;
}

void RectSelectButton::SetClickUp()
{
	m_bMouseDown = FALSE;
	m_bMouseUp = TRUE;
}